﻿
//----------------------------------------------------------
//----------------------------------------------------------
Ext.unaidedHistogram = function(config){
    //alert();
    Ext.apply(this,config);
    this.init();
}

Ext.extend(Ext.unaidedHistogram,Ext.unaidedHistogram,{
    init:function(){
    },
    clear : function(){
    },
    getDataObj : function(o,pList){
        if(pList.indexOf(".")==-1) return o[pList];
        var ps = pList.split(".");
        pList = pList.replace(ps[0]+".","");
        return this.getDataObj(o[ps[0]],pList);
    },

    load : function(){
        var config = {};
        
        //-----------------------------------------------------------
        //处理入参
        //-----------------------------------------------------------
        if(arguments.length!=0){
            var a = arguments[0];
            if(typeof a =="object"){
                Ext.apply(this,a);
                config = a;
            }else{
                this.url = a;
            }
        }
        
        if(this.url==undefined || this.url=="") {
            Ext.Msg.alert("信息：","没有找到关于数据源的定义！无法获取数据！");
            return;
        }

        //-----------------------------------------------------------
        this.clear();
        
        //-----------------------------------------------------------
        //-----------------------------------------------------------
        if(config.reader == undefined)      config.reader = this.reader;
        
        var reader = config.reader;
        if(reader==undefined){
            Ext.Msg.alert("信息：","没有找到关于数据解析对象的定义！无法解析数据！");
            return;
        }
        
        reader.yCoordinate =(reader.yCoordinate instanceof Array)?reader.yCoordinate:[reader.yCoordinate];
                
        var theObj = this;
        var _container = document.getElementById(this.renderTo);
                
        var _callback = function(flag,getData){
            if(reader.type=="JsonReader"){
                
                //------------------------------------------------------------
                //处理表格显示的一些全局设置
                //------------------------------------------------------------
                var table_align    = (theObj.tabAlign!=undefined)?theObj.tabAlign:"center";
                var table_colWidth = (theObj.tabColWidth!=undefined)?theObj.tabColWidth:[50,100,360,100];
                var HSG_maxWidth   = table_colWidth[2]-10;
                var tableWidth     = eval(table_colWidth.join('+'))+2*4;

                //------------------------------------------------------------
                var _html = new Sail.stringBuffer();
                _html.append("<table align='"+table_align+"' border='0' cellpadding='0' cellspacing='2' width="+tableWidth+">");

                //------------------------------------------------------------
                var lineData = theObj.getDataObj(getData,reader.root);
                
                lineData = (lineData instanceof Array)?lineData:(lineData==undefined)?[]:[lineData];
                
                var tmpYValue = [];
                for(var i=0;i<lineData.length;i++){
                    for(var j=0;j<reader.yCoordinate.length;j++){           //计算Y坐标轴最大值用临时数组
                        tmpYValue.push(lineData[i][reader.yCoordinate[j]] == undefined ? 0 : lineData[i][reader.yCoordinate[j]]);
                    }
                }
                var maxValue = eval("Math.max("+tmpYValue.join(',')+")");

                //------------------------------------------------------------
                //------------------------------------------------------------
                for(var i=0;i<lineData.length;i++){
                    var _name = lineData[i][reader.xCoordinate];
                    _name = _name.replace('卷','<br>卷');

                    var _titles = [];
                    if(config.titleRender!=undefined && typeof config.titleRender == "function"){
                        for(var j=0;j<3;j++){
                            var tmpT = lineData[i][reader.yCoordinate[j]];
                            _titles.push(config.titleRender(lineData[i][reader.xCoordinate],lineData[i][reader.yCoordinate[j]],reader.yCoordinate[j],lineData[i]));
                        }
                    }
                    
                    //--------------------------------------------------------
                    _value = lineData[i][reader.yCoordinate[0]];
                    _value = (_value == undefined ? 0 : _value);
                    _width = parseInt((_value/maxValue)*HSG_maxWidth,10);
                    _html.append("<tr>");
                    _html.append("<td rowspan=3 width="+table_colWidth[0]+" class='hh_td1'>"+_name+"</td>");
                    _html.append("<td width="+table_colWidth[1]+" class='hh_td2'>本月计划量</td>");
                    _html.append("<td ><div class='hh_bgdiv_1' style='background-position-x:"+(_width-1000)+";'>");
                    
                    _text = (_titles.length==0)?_value:_titles[0];
                    _html.append("<div style='position:relative;left:"+(_width+5)+"'>"+_text+"</div>");
                    
                    _html.append("</div></td><td width=100>&nbsp;</td></tr>");

                    //--------------------------------------------------------
                    _value = lineData[i][reader.yCoordinate[1]];
                    _value = (_value == undefined ? 0 : _value);
                    _width = parseInt((_value/maxValue)*HSG_maxWidth,10);
                    _html.append("<tr><td class='hh_td2'>累计当日计划量</td>");
                    _html.append("<td ><div class='hh_bgdiv_2' style='background-position-x:"+(_width-1000)+";'>");

                    _text = (_titles.length==0)?_value:_titles[1];
                    _html.append("<div style='position:relative;left:"+(_width+5)+"'>"+_text+"</div>");
                    _html.append("</div></td></tr>");

                    //--------------------------------------------------------
                    _value = lineData[i][reader.yCoordinate[2]];
                    _value = (_value == undefined ? 0 : _value);
                    _width = parseInt((_value/maxValue)*HSG_maxWidth,10);
                    _html.append("<tr><td class='hh_td2'>累计当日产量</td>");
                    _html.append("<td ><div class='hh_bgdiv_3' style='background-position-x:"+(_width-1000)+";'>");

                    _text = (_titles.length==0)?_value:_titles[2];
                    _html.append("<div style='position:relative;left:"+(_width+5)+"'>"+_text+"</div>");
                    _html.append("</div></td></tr>");
                    _html.append("<tr height=6><td></td></tr>");

                }
                
                _container.innerHTML = _html.toString();
                //------------------------------------------------------------
                //------------------------------------------------------------
                //debugger;
                
                
            }

        };
        
        this.postData(this.url,{},_callback);
        
    },
    postData : function(_url,postData,successInfo,errorInfo){
        if(_url=="") {
            Ext.Msg.alert("信息","没有找到url！");
            return;
        }
        this.showLoadMask('正在处理数据，请等待........');
        
        var theObj = this;
        Ext.Ajax.request({
            url: _url,method: 'POST',params: postData,
            success: function(action) {
                theObj.hideLoadMask();
                //debugger;
                var getResponse = eval("("+action.responseText+")"); 
                var flag = getResponse.dataset.response.success;
                
                if(typeof successInfo =="function") {
                    successInfo.call(this,flag,getResponse);
                }else{
                    Ext.Msg.alert('提示：', successInfo); return true;
                }
            },
            failure: function(action) {
                theObj.hideLoadMask();
                var _status = action.status; 

                if(typeof errorInfo =="function") {
                    errorInfo.call(this,false,_status);
                }else{
                    Ext.Msg.alert('提示：', '获取数据失败！'); return false;
                }
                
                //Ext.Msg.alert('提示：', '获取数据失败！'); return false;
                
            }
        });
    },
    showLoadMask : function(_msg){
        //var msg = (arguments.length==0)?"保存数据，请稍后......":arguments[1];
        //this.loadMask = (this.loadMask==undefined)?new Ext.LoadMask(Ext.get('mainDiv'),{msgCls:'x-mask-saveing',msg:_msg}):this.loadMask;

        //this.loadMask = new Ext.LoadMask(Ext.get('document'),{msgCls:'x-mask-saveing',msg:_msg});
        //this.loadMask.show();

    },
    hideLoadMask : function(){
        //this.loadMask.hide();
    }    
});